import java.io.*;

/**
 * @author dragon
 * @version 1.0
 * @description 数组的深层复制
 * @date created in 2018-11-13
 */
public class ArrayDeepCopy {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person[] arr = new Person[3];
        Person dragon = new Person("dragon", 12);
        Student s1 = new Student("1班");
        dragon.setS(s1);
        Person mars = new Person("mars", 10);
        Student s2 = new Student("2班");
        mars.setS(s2);
        Person pig = new Person("pig", 20);
        Student s3 = new Student("3班");
        pig.setS(s3);
        arr[0] = dragon;
        arr[1] = mars;
        arr[2] = pig;
        Person[] newArr = deepCopy(arr);
        System.out.println(arr[0].getS().getClassesName());
        System.out.println(newArr[0].getS().getClassesName());
    }

    public static <T extends Serializable> T[] deepCopy(T[] array) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(array);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        return (T[]) in.readObject();
    }
}
